<?php

class GoogleMapsTraceRoute_Widget extends WP_Widget {
	protected $_gmtr;

    function GoogleMapsTraceRoute_Widget() {
        $this->WP_Widget(false, 'GMTR', array('description' => 'Google Maps Trace Route'));
		$this->_gmtr = $GLOBALS['gmtr'];
    }

	function widget($args, $instance) {
		extract( $args );
		$title = apply_filters('widget_title', $instance['title']);
        ?>
			<?php echo $before_widget; ?>
			<?php
				if ( $title ) echo $before_title . $title . $after_title;
				echo $this->_gmtr->show_map($instance);
			?>
			<?php echo $after_widget; ?>
        <?php
	}

    function update($new_instance, $old_instance) {
        return $new_instance;
    }

    function form($instance) {
		$title = $instance['title'];
		$width = $this->_gmtr->get_width($instance['width']);
		$height = $this->_gmtr->get_height($instance['height']);

		$opacity = $this->_gmtr->get_opacity($instance['opacity']);
		$maptype = $this->_gmtr->get_maptype($instance['maptype']);
		$points = $this->_gmtr->get_zoomcounter($instance['points']);
		$color = $this->_gmtr->get_color($instance['color']);
		$mapname = (string)$instance['mapname'];
		$showroute = isset($instance['showroute']) ? !!$instance['showroute'] : 1;

        echo '<p><label for="'.$this->get_field_id('title').'">Title: <input class="widefat" id="'.$this->get_field_id('title').'" name="'.$this->get_field_name('title').'" type="text" value="'.esc_attr($title).'" /></label></p>';
        echo '<p>';
		echo '<table><tbody>';
        echo '<tr><th style="text-align: right"><label for="'.$this->get_field_id('points').'">Last points:</label></th><td><input style="width: 6em" id="'.$this->get_field_id('points').'" name="'.$this->get_field_name('points').'" type="text" value="'.esc_attr($points).'" /></td></tr>';
        echo '<tr><th style="text-align: right"><label for="'.$this->get_field_id('showroute').'">Show route:</label></th><td><select id="'.$this->get_field_id('showroute').'" name="'.$this->get_field_name('showroute').'">';
		echo '<option value="1"'.($showroute ? ' selected="selected"' : '').'>Yes</option>';
		echo '<option value="0"'.($showroute ? '': ' selected="selected"').'>No</option>';
		echo '</select></td></tr>';
        echo '<tr><th style="text-align: right"><label for="'.$this->get_field_id('color').'">Color:</label></th><td><input style="width: 6em" id="'.$this->get_field_id('color').'" name="'.$this->get_field_name('color').'" type="text" value="'.esc_attr($color).'" /></td></tr>';
        echo '<tr><th style="text-align: right"><label for="'.$this->get_field_id('opacity').'">Opacity:</label></th><td><select id="'.$this->get_field_id('opacity').'" name="'.$this->get_field_name('opacity').'">';
		for($i=10; $i<=100;$i+=10) {
			echo '<option value="'.$i.'"'.($opacity == $i ? ' selected="selected"' : '').'>'.$i.'%</option>';
		}
		echo '</select></td></tr>';
        echo '<tr><th style="text-align: right"><label for="'.$this->get_field_id('maptype').'">Map type:</label></th><td><select id="'.$this->get_field_id('maptype').'" name="'.$this->get_field_name('maptype').'">';
		foreach(array('HYBRID', 'ROADMAP', 'SATELLITE', 'TERRAIN') as $type)
			echo '<option value="'.$type.'"'.($maptype == $type ? ' selected="selected"' : '').'>'.ucwords(strtolower($type)).'</option>';
		echo '</select></td></tr>';
		echo '<tr><th style="text-align: right"><label for="'.$this->get_field_id('width').'">Map width:</label></th><td><input style="width: 6em" id="'.$this->get_field_id('width').'" name="'.$this->get_field_name('width').'" type="text" value="'.esc_attr($width).'" /></td></tr>';
        echo '<tr><th style="text-align: right"><label for="'.$this->get_field_id('height').'">Map height:</label></th><td><input style="width: 6em" id="'.$this->get_field_id('height').'" name="'.$this->get_field_name('height').'" type="text" value="'.esc_attr($height).'" /></td></tr>';
        echo '<tr><th style="text-align: right"><label for="'.$this->get_field_id('mapname').'">Map:</label></th><td><select id="'.$this->get_field_id('mapname').'" name="'.$this->get_field_name('mapname').'">';
		$maps = array_merge(array(''), $this->_gmtr->get_mapnames());
		foreach($maps as $map) {
			echo '<option value="'.$map.'"'.($mapname == $map ? ' selected="selected"' : '').'>'.($map == '' ? '(all)' : ucwords(strtolower($map))).'</option>';
		}
		echo '</select></td></tr>';
		echo '</tbody></table>';
    }
}

?>